<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-scout" tabindex="-1"><a class="header-anchor" href="#laravel-scout"><span>Laravel Scout</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#queueing">队列化</a></li>
</ul>
</li>
<li><a href="#driver-prerequisites">驱动程序先决条件</a>
<ul>
<li><a href="#algolia">Algolia</a></li>
<li><a href="#meilisearch">Meilisearch</a></li>
<li><a href="#typesense">Typesense</a></li>
</ul>
</li>
<li><a href="#configuration">配置</a>
<ul>
<li><a href="#configuring-model-indexes">配置模型索引</a></li>
<li><a href="#configuring-searchable-data">配置可搜索数据</a></li>
<li><a href="#configuring-the-model-id">配置模型 ID</a></li>
<li><a href="#configuring-search-engines-per-model">配置每个模型的搜索引擎</a></li>
<li><a href="#identifying-users">识别用户</a></li>
</ul>
</li>
<li><a href="#database-and-collection-engines">数据库/集合引擎</a>
<ul>
<li><a href="#database-engine">数据库引擎</a></li>
<li><a href="#collection-engine">集合引擎</a></li>
</ul>
</li>
<li><a href="#indexing">索引</a>
<ul>
<li><a href="#batch-import">批量导入</a></li>
<li><a href="#adding-records">添加记录</a></li>
<li><a href="#updating-records">更新记录</a></li>
<li><a href="#removing-records">移除记录</a></li>
<li><a href="#pausing-indexing">暂停索引</a></li>
<li><a href="#conditionally-searchable-model-instances">有条件地搜索模型实例</a></li>
</ul>
</li>
<li><a href="#searching">搜索</a>
<ul>
<li><a href="#where-clauses">Where 子句</a></li>
<li><a href="#pagination">分页</a></li>
<li><a href="#soft-deleting">软删除</a></li>
<li><a href="#customizing-engine-searches">自定义引擎搜索</a></li>
</ul>
</li>
<li><a href="#custom-engines">自定义引擎</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/scout" target="_blank" rel="noopener noreferrer">Laravel Scout</a> 提供了一个简单的、基于驱动程序的解决方案，用于为你的<a href="https://learnku.com/docs/laravel/11.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent 模型</a>添加全文搜索功能。使用模型观察者，Scout 将自动保持你的搜索索引与你的 Eloquent 记录同步。</p>
<p>目前，Scout 配备了 <a href="https://www.algolia.com/" target="_blank" rel="noopener noreferrer">Algolia</a>、<a href="https://www.meilisearch.com/" target="_blank" rel="noopener noreferrer">Meilisearch</a>、<a href="https://typesense.org/" target="_blank" rel="noopener noreferrer">Typesense</a> 和 MySQL/PostgreSQL（<code v-pre>database</code>）驱动程序。此外，Scout 还包括一个“集合”驱动程序，专为本地开发使用而设计，不需要任何外部依赖或第三方服务。此外，编写自定义驱动程序很简单，你可以自由地使用你自己的搜索实现扩展 Scout。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>首先，通过 Composer 包管理器安装 Scout：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/scout</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>安装完 Scout 后，你应该使用 <code v-pre>vendor:publish</code> Artisan 命令发布 Scout 配置文件。此命令将会把 <code v-pre>scout.php</code> 配置文件发布到你的应用程序的 <code v-pre>config</code> 目录中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--provider</span><span class="token operator">=</span><span class="token string">"Laravel\Scout\ScoutServiceProvider"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>最后，将 <code v-pre>Laravel\Scout\Searchable</code> trait 添加到你想要进行搜索的模型中。这个 trait 将注册一个模型观察者，它将自动保持模型与你的搜索驱动程序同步：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Post</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Searchable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="队列化" tabindex="-1"><a class="header-anchor" href="#队列化"><span>队列化</span></a></h3>
<p>虽然不是严格要求使用 Scout，但在使用这个库之前，你应该强烈考虑配置一个<a href="https://learnku.com/docs/laravel/11.x/queues" target="_blank" rel="noopener noreferrer">队列驱动程序</a>。运行队列工作者将允许 Scout 将所有同步模型信息到搜索索引的操作排入队列，为你的应用程序的 Web 接口提供更好的响应时间。</p>
<p>一旦配置了队列驱动程序，请将 <code v-pre>config/scout.php</code> 配置文件中的 <code v-pre>queue</code> 选项的值设置为 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>即使 <code v-pre>queue</code> 选项设置为 <code v-pre>false</code>，也要记住，一些 Scout 驱动程序（如 Algolia 和 Meilisearch）总是异步索引记录。这意味着，即使在 Laravel 应用程序中完成了索引操作，搜索引擎本身可能不会立即反映新的和更新的记录。</p>
<p>要指定 Scout 作业使用的连接和队列，你可以将 <code v-pre>queue</code> 配置选项定义为一个数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'connection'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'scout'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，如果你自定义了 Scout 作业使用的连接和队列，你应该运行一个队列工作者来处理该连接和队列上的作业：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan queue<span class="token punctuation">:</span>work redis <span class="token operator">--</span>queue<span class="token operator">=</span>scout</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="驱动程序先决条件" tabindex="-1"><a class="header-anchor" href="#驱动程序先决条件"><span>驱动程序先决条件</span></a></h2>
<h3 id="algolia" tabindex="-1"><a class="header-anchor" href="#algolia"><span>Algolia</span></a></h3>
<p>当使用 Algolia 驱动程序时，你应该在 <code v-pre>config/scout.php</code> 配置文件中配置你的 Algolia <code v-pre>id</code> 和 <code v-pre>secret</code> 凭据。一旦配置了你的凭据，你还需要通过 Composer 包管理器安装 Algolia PHP SDK：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require algolia/algoliasearch-client-php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="meilisearch" tabindex="-1"><a class="header-anchor" href="#meilisearch"><span>Meilisearch</span></a></h3>
<p><a href="https://www.meilisearch.com/" target="_blank" rel="noopener noreferrer">Meilisearch</a> 是一个极快的开源搜索引擎。如果你不确定如何在本地安装 Meilisearch，你可以使用 <a href="https://learnku.com/docs/laravel/11.x/sail#meilisearch" target="_blank" rel="noopener noreferrer">Laravel Sail</a>，这是 Laravel 官方支持的 Docker 开发环境。</p>
<p>当使用 Meilisearch 驱动程序时，你需要通过 Composer 包管理器安装 Meilisearch PHP SDK：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require meilisearch/meilisearch-php http-interop/http-factory-guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，在应用程序的 <code v-pre>.env</code> 文件中设置 <code v-pre>SCOUT_DRIVER</code> 环境变量以及你的 Meilisearch <code v-pre>host</code> 和 <code v-pre>key</code> 凭据：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">SCOUT_DRIVER</span><span class="token punctuation">=</span><span class="token value attr-value">meilisearch</span></span>
<span class="line"><span class="token key attr-name">MEILISEARCH_HOST</span><span class="token punctuation">=</span><span class="token value attr-value">http://127.0.0.1:7700</span></span>
<span class="line"><span class="token key attr-name">MEILISEARCH_KEY</span><span class="token punctuation">=</span><span class="token value attr-value">masterKey</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关 Meilisearch 的更多信息，请参考<a href="https://docs.meilisearch.com/learn/getting_started/quick_start.html" target="_blank" rel="noopener noreferrer">Meilisearch 文档</a>。</p>
<p>此外，你应确保安装一个与你的 Meilisearch 二进制版本兼容的 <code v-pre>meilisearch/meilisearch-php</code> 版本，通过查看<a href="https://github.com/meilisearch/meilisearch-php#-compatibility-with-meilisearch" target="_blank" rel="noopener noreferrer">Meilisearch 关于二进制兼容性的文档</a>。</p>
<blockquote>
<p><strong>注意</strong><br>
在升级使用 Meilisearch 的应用程序的 Scout 时，你应始终<a href="https://github.com/meilisearch/Meilisearch/releases" target="_blank" rel="noopener noreferrer">查看 Meilisearch 服务本身的任何其他重大变更</a>。</p>
</blockquote>
<h3 id="typesense" tabindex="-1"><a class="header-anchor" href="#typesense"><span>Typesense</span></a></h3>
<p><a href="https://typesense.org/" target="_blank" rel="noopener noreferrer">Typesense</a> 是一个极快的开源搜索引擎，支持关键字搜索、语义搜索、地理位置搜索和向量搜索。</p>
<p>你可以选择 <a href="https://typesense.org/docs/guide/install-typesense.html#option-2-local-machine-self-hosting" target="_blank" rel="noopener noreferrer">自托管</a> Typesense，或使用 <a href="https://cloud.typesense.org/" target="_blank" rel="noopener noreferrer">Typesense Cloud</a>。</p>
<p>要开始使用 Typesense 和 Scout，通过 Composer 包管理器安装 Typesense PHP SDK：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require typesense/typesense-php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，在应用程序的 <code v-pre>.env</code> 文件中设置 <code v-pre>SCOUT_DRIVER</code> 环境变量以及你的 Typesense 主机和 API 密钥凭据：</p>
<div class="language-env line-numbers-mode" data-highlighter="prismjs" data-ext="env" data-title="env"><pre v-pre class="language-env"><code><span class="line">SCOUT_DRIVER=typesense</span>
<span class="line">TYPESENSE_API_KEY=masterKey</span>
<span class="line">TYPESENSE_HOST=localhost</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你还可以指定安装的端口、路径和协议：</p>
<div class="language-env line-numbers-mode" data-highlighter="prismjs" data-ext="env" data-title="env"><pre v-pre class="language-env"><code><span class="line">TYPESENSE_PORT=8108</span>
<span class="line">TYPESENSE_PATH=</span>
<span class="line">TYPESENSE_PROTOCOL=http</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以在应用程序的 <code v-pre>config/scout.php</code> 配置文件中找到有关 Typesense 集合的额外设置和模式定义。有关 Typesense 的更多信息，请参考 <a href="https://typesense.org/docs/guide/#quick-start" target="_blank" rel="noopener noreferrer">Typesense 文档</a>。</p>
<h4 id="准备数据以在-typesense-中存储" tabindex="-1"><a class="header-anchor" href="#准备数据以在-typesense-中存储"><span>准备数据以在 Typesense 中存储</span></a></h4>
<p>在使用 Typesense 时，你的可搜索模型必须定义一个 <code v-pre>toSearchableArray</code> 方法，将你的模型的主键转换为字符串，并将创建日期转换为 UNIX 时间戳：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取模型的可索引数据数组。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSearchableArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">array_merge</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'created_at'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">created_at</span><span class="token operator">-></span><span class="token property">timestamp</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还应该在应用程序的 <code v-pre>config/scout.php</code> 文件中定义你的 Typesense 集合模式。集合模式描述了通过 Typesense 可搜索的每个字段的数据类型。有关所有可用模式选项的更多信息，请参考 <a href="https://typesense.org/docs/latest/api/collections.html#schema-parameters" target="_blank" rel="noopener noreferrer">Typesense 文档</a>。</p>
<p>如果需要在定义后更改 Typesense 集合的模式，你可以运行 <code v-pre>scout:flush</code> 和 <code v-pre>scout:import</code>，这将删除所有现有的索引数据并重新创建模式。或者，你可以使用 Typesense 的 API 在不删除任何索引数据的情况下修改集合的模式。</p>
<p>如果你的可搜索模型支持软删除，你应该在应用程序的 <code v-pre>config/scout.php</code> 配置文件中为模型对应的 Typesense 模式定义一个 <code v-pre>__soft_deleted</code> 字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'collection-schema'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'fields'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">            <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'__soft_deleted'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'int32'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'optional'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="动态搜索参数" tabindex="-1"><a class="header-anchor" href="#动态搜索参数"><span>动态搜索参数</span></a></h4>
<p>Typesense 允许你在执行搜索操作时通过 <code v-pre>options</code> 方法动态修改你的<a href="https://typesense.org/docs/latest/api/search.html#search-parameters" target="_blank" rel="noopener noreferrer">搜索参数</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Todo</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Todo</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Groceries'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">options</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'query_by'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'title, description'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<h3 id="配置模型索引" tabindex="-1"><a class="header-anchor" href="#配置模型索引"><span>配置模型索引</span></a></h3>
<p>每个 Eloquent 模型都与一个特定的搜索“索引”同步，其中包含该模型的所有可搜索记录。换句话说，你可以将每个索引视为一个 MySQL 表。默认情况下，每个模型将持久化到与模型典型“表”名称匹配的索引中。通常，这是模型名称的复数形式；但是，你可以通过在模型上重写 <code v-pre>searchableAs</code> 方法来自定义模型的索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Post</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取与模型关联的索引名称。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">searchableAs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'posts_index'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="配置可搜索数据" tabindex="-1"><a class="header-anchor" href="#配置可搜索数据"><span>配置可搜索数据</span></a></h3>
<p>默认情况下，给定模型的整个 <code v-pre>toArray</code> 表单将持久化到其搜索索引中。如果你想要自定义同步到搜索索引的数据，可以在模型上重写 <code v-pre>toSearchableArray</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Post</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取模型的可索引数据数组。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSearchableArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$array</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 自定义数据数组...</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$array</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一些搜索引擎（如 Meilisearch）仅会在正确类型的数据上执行过滤操作（<code v-pre>&gt;</code>, <code v-pre>&lt;</code> 等）。因此，在使用这些搜索引擎并自定义可搜索数据时，应确保将数字值转换为其正确的类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSearchableArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token keyword type-casting">float</span><span class="token punctuation">)</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">price</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="配置可过滤数据和索引设置-meilisearch" tabindex="-1"><a class="header-anchor" href="#配置可过滤数据和索引设置-meilisearch"><span>配置可过滤数据和索引设置（Meilisearch）</span></a></h4>
<p>与 Scout 的其他驱动程序不同，Meilisearch 需要预定义索引搜索设置，例如可过滤属性、可排序属性和<a href="https://docs.meilisearch.com/reference/api/settings.html" target="_blank" rel="noopener noreferrer">其他支持的设置字段</a>。</p>
<p>可过滤属性是在调用 Scout 的 <code v-pre>where</code> 方法时计划进行过滤的任何属性，而可排序属性是在调用 Scout 的 <code v-pre>orderBy</code> 方法时计划进行排序的任何属性。要定义索引设置，请调整应用程序的 <code v-pre>scout</code> 配置文件中 <code v-pre>meilisearch</code> 配置条目的 <code v-pre>index-settings</code> 部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Flight</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'meilisearch'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MEILISEARCH_HOST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'http://localhost:7700'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MEILISEARCH_KEY'</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'index-settings'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'filterableAttributes'</span><span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'sortableAttributes'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// 其他设置字段...</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Flight</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'filterableAttributes'</span><span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'destination'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'sortableAttributes'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'updated_at'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果给定索引下的模型支持软删除，并包含在 <code v-pre>index-settings</code> 数组中，Scout 将自动在该索引上包含对软删除模型的过滤支持。如果对于支持软删除的模型索引没有其他可过滤或可排序属性需要定义，你可以简单地为该模型在 <code v-pre>index-settings</code> 数组中添加一个空条目：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'index-settings'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">Flight</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在配置应用程序的索引设置后，你必须调用 <code v-pre>scout:sync-index-settings</code> Artisan 命令。该命令将通知 Meilisearch 您当前配置的索引设置。为方便起见，你可能希望将此命令作为部署过程的一部分：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan scout:sync-index-settings</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置模型-id" tabindex="-1"><a class="header-anchor" href="#配置模型-id"><span>配置模型 ID</span></a></h3>
<p>默认情况下，Scout 将使用模型的主键作为存储在搜索索引中的模型唯一 ID / 键。如果需要自定义此行为，可以在模型上重写 <code v-pre>getScoutKey</code> 和 <code v-pre>getScoutKeyName</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用于索引模型的值。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getScoutKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">mixed</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用于索引模型的键名。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getScoutKeyName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">mixed</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="配置每个模型的搜索引擎" tabindex="-1"><a class="header-anchor" href="#配置每个模型的搜索引擎"><span>配置每个模型的搜索引擎</span></a></h3>
<p>在搜索时，Scout 通常会使用应用程序 <code v-pre>scout</code> 配置文件中指定的默认搜索引擎。但是，可以通过在模型上重写 <code v-pre>searchableUsing</code> 方法来更改特定模型的搜索引擎：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Engines<span class="token punctuation">\</span>Engine</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>EngineManager</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Searchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用于索引模型的引擎。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">searchableUsing</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Engine</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token class-name static-context">EngineManager</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">engine</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'meilisearch'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="识别用户" tabindex="-1"><a class="header-anchor" href="#识别用户"><span>识别用户</span></a></h3>
<p>Scout 还允许你在使用 <a href="https://algolia.com/" target="_blank" rel="noopener noreferrer">Algolia</a> 时自动识别用户。将经过身份验证的用户与搜索操作关联起来，在查看 Algolia 仪表板中的搜索分析时可能会很有帮助。你可以通过在应用的 <code v-pre>.env</code> 文件中将 <code v-pre>SCOUT_IDENTIFY</code> 环境变量定义为 <code v-pre>true</code> 来启用用户识别：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">SCOUT_IDENTIFY</span><span class="token punctuation">=</span><span class="token value attr-value">true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>启用此功能还将传递请求的 IP 地址和经过身份验证的用户的主要标识符到 Algolia，以便将这些数据与用户发出的任何搜索请求关联起来。</p>
<h2 id="数据库-集合引擎" tabindex="-1"><a class="header-anchor" href="#数据库-集合引擎"><span>数据库 / 集合引擎</span></a></h2>
<h3 id="数据库引擎" tabindex="-1"><a class="header-anchor" href="#数据库引擎"><span>数据库引擎</span></a></h3>
<blockquote>
<p><strong>注意</strong><br>
当前数据库引擎支持 MySQL 和 PostgreSQL。</p>
</blockquote>
<p>如果你的应用与中小型数据库交互或工作负载较轻，你可能会发现使用 Scout 的 &quot;database&quot; 引擎更方便。数据库引擎将在从现有数据库筛选结果以确定查询的适用搜索结果时使用 &quot;where like&quot; 子句和全文索引。</p>
<p>要使用数据库引擎，你可以简单地将 <code v-pre>SCOUT_DRIVER</code> 环境变量的值设置为 <code v-pre>database</code>，或者直接在应用的 <code v-pre>scout</code> 配置文件中指定 <code v-pre>database</code> 驱动程序：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">SCOUT_DRIVER</span><span class="token punctuation">=</span><span class="token value attr-value">database</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦你将数据库引擎指定为首选驱动程序，你必须<a href="#%E9%85%8D%E7%BD%AE%E5%8F%AF%E6%90%9C%E7%B4%A2%E7%9A%84%E6%95%B0%E6%8D%AE">配置可搜索的数据</a>。然后，你可以开始对你的模型执行<a href="#%E6%90%9C%E7%B4%A2">搜索查询</a>。使用数据库引擎时，不需要进行搜索引擎索引，例如需要为 Algolia、Meilisearch 或 Typesense 索引播种的索引。</p>
<h4 id="自定义数据库搜索策略" tabindex="-1"><a class="header-anchor" href="#自定义数据库搜索策略"><span>自定义数据库搜索策略</span></a></h4>
<p>默认情况下，数据库引擎将针对你已经<a href="#%E9%85%8D%E7%BD%AE%E5%8F%AF%E6%90%9C%E7%B4%A2%E7%9A%84%E6%95%B0%E6%8D%AE">配置为可搜索的</a>每个模型属性执行 「where like」 查询。然而，在某些情况下，这可能导致性能不佳。因此，可以配置数据库引擎的搜索策略，以便一些指定的列利用全文搜索查询，或者仅使用 &quot;where like&quot; 约束来搜索字符串的前缀（<code v-pre>example%</code>），而不是在整个字符串内进行搜索（<code v-pre>%example%</code>）。</p>
<p>要定义这种行为，你可以为模型的 <code v-pre>toSearchableArray</code> 方法分配 PHP 属性。未分配额外搜索策略行为的任何列将继续使用默认的 「where like」 策略：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Attributes<span class="token punctuation">\</span>SearchUsingFullText</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Attributes<span class="token punctuation">\</span>SearchUsingPrefix</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取模型的可索引数据数组。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line"> */</span></span>
<span class="line"><span class="token attribute"><span class="token delimiter punctuation">#[</span><span class="token attribute-content"><span class="token attribute-class-name class-name">SearchUsingPrefix</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span><span class="token delimiter punctuation">]</span></span></span>
<span class="line"><span class="token attribute"><span class="token delimiter punctuation">#[</span><span class="token attribute-content"><span class="token attribute-class-name class-name">SearchUsingFullText</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bio'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span><span class="token delimiter punctuation">]</span></span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSearchableArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'bio'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">bio</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
在指定某个列应使用全文搜索查询约束之前，请确保该列已被分配了<a href="https://learnku.com/docs/laravel/11.x/migrations#available-index-types" target="_blank" rel="noopener noreferrer">全文索引</a>。</p>
</blockquote>
<h3 id="集合引擎" tabindex="-1"><a class="header-anchor" href="#集合引擎"><span>集合引擎</span></a></h3>
<p>虽然你可以在本地开发过程中自由使用 Algolia、Meilisearch 或 Typesense 搜索引擎，但你可能会发现使用 「collection」 引擎更为方便。集合引擎将在从现有数据库筛选结果以确定查询的适用搜索结果时使用 「where」 子句和集合过滤。使用此引擎时，不需要对可搜索的模型进行 「索引」，因为它们将直接从你的本地数据库中检索。</p>
<p>要使用集合引擎，你可以简单地将 <code v-pre>SCOUT_DRIVER</code> 环境变量的值设置为 <code v-pre>collection</code>，或者直接在应用的 <code v-pre>scout</code> 配置文件中指定 <code v-pre>collection</code> 驱动程序：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">SCOUT_DRIVER</span><span class="token punctuation">=</span><span class="token value attr-value">collection</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦你将集合驱动程序指定为首选驱动程序，你可以开始对你的模型执行<a href="#%E6%90%9C%E7%B4%A2">搜索查询</a>。使用集合引擎时，不需要进行搜索引擎索引，例如需要为 Algolia、Meilisearch 或 Typesense 索引播种的索引。</p>
<h4 id="与数据库引擎的区别" tabindex="-1"><a class="header-anchor" href="#与数据库引擎的区别"><span>与数据库引擎的区别</span></a></h4>
<p>乍一看，“database” 和 “collections” 引擎非常相似。它们都直接与你的数据库交互以检索搜索结果。然而，集合引擎不使用全文索引或 <code v-pre>LIKE</code> 子句来查找匹配的记录。相反，它获取所有可能的记录，并使用 Laravel 的 <code v-pre>Str::is</code> 辅助函数来确定搜索字符串是否存在于模型属性值中。</p>
<p>集合引擎是最通用的搜索引擎，因为它适用于 Laravel 支持的所有关系数据库（包括 SQLite 和 SQL Server）；然而，它不如 Scout 的数据库引擎高效。</p>
<h2 id="索引" tabindex="-1"><a class="header-anchor" href="#索引"><span>索引</span></a></h2>
<h3 id="批量导入" tabindex="-1"><a class="header-anchor" href="#批量导入"><span>批量导入</span></a></h3>
<p>如果你将 Scout 安装到现有项目中，可能已经有需要导入到你的索引中的数据库记录。Scout 提供了一个 <code v-pre>scout:import</code> Artisan 命令，你可以使用它将所有现有记录导入到你的搜索索引中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan scout:import <span class="token string">"App\Models\Post"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>flush</code> 命令可用于从你的搜索索引中删除模型的所有记录：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan scout:flush <span class="token string">"App\Models\Post"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="修改导入查询" tabindex="-1"><a class="header-anchor" href="#修改导入查询"><span>修改导入查询</span></a></h4>
<p>如果你想修改用于检索所有模型以进行批量导入的查询，你可以在你的模型上定义一个 <code v-pre>makeAllSearchableUsing</code> 方法。这是一个很好的地方，可以在导入模型之前添加任何可能需要的急切关联加载：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Builder</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 修改用于在使所有模型可搜索时检索模型的查询。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">makeAllSearchableUsing</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$query</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Builder</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'author'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
当使用队列批量导入模型时，<code v-pre>makeAllSearchableUsing</code> 方法可能不适用。当模型集合由作业处理时，关系<a href="https://learnku.com/docs/laravel/11.x/queues#handling-relationships" target="_blank" rel="noopener noreferrer">不会被恢复</a>。</p>
</blockquote>
<h3 id="添加记录" tabindex="-1"><a class="header-anchor" href="#添加记录"><span>添加记录</span></a></h3>
<p>一旦你向模型添加了 <code v-pre>Laravel\Scout\Searchable</code> 特性，你只需对模型实例执行 <code v-pre>save</code> 或 <code v-pre>create</code> 操作，它将自动添加到你的搜索索引中。如果你已经配置 Scout <a href="#%E9%98%9F%E5%88%97%E5%8C%96">使用队列</a>，此操作将由你的队列工作程序在后台执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$order</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$order</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过查询添加记录" tabindex="-1"><a class="header-anchor" href="#通过查询添加记录"><span>通过查询添加记录</span></a></h4>
<p>如果你想通过 Eloquent 查询将一组模型添加到你的搜索索引中，你可以在 Eloquent 查询上链式调用 <code v-pre>searchable</code> 方法。<code v-pre>searchable</code> 方法将<a href="https://learnku.com/docs/laravel/11.x/eloquent#chunking-results" target="_blank" rel="noopener noreferrer">分块处理查询结果</a>，并将记录添加到你的搜索索引中。同样，如果你已经配置 Scout 使用队列，所有的分块将由你的队列工作者在后台导入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">searchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以在 Eloquent 关联实例上调用 <code v-pre>searchable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">orders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">searchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，如果你已经在内存中有一组 Eloquent 模型实例，你可以在集合实例上调用 <code v-pre>searchable</code> 方法，将模型实例添加到它们对应的索引中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span><span class="token operator">-></span><span class="token function">searchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
<code v-pre>searchable</code> 方法可以被视为一种 &quot;upsert&quot; 操作。换句话说，如果模型记录已经存在于你的索引中，它将被更新。如果它不存在于搜索索引中，它将被添加到索引中。</p>
</blockquote>
<h3 id="更新记录" tabindex="-1"><a class="header-anchor" href="#更新记录"><span>更新记录</span></a></h3>
<p>要更新可搜索的模型，你只需更新模型实例的属性并将模型 <code v-pre>save</code> 到你的数据库中。Scout 将自动将更改持久化到你的搜索索引中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$order</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 更新订单...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$order</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以在 Eloquent 查询实例上调用 <code v-pre>searchable</code> 方法来更新一组模型。如果这些模型不存在于你的搜索索引中，它们将被创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">searchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想更新关系中所有模型的搜索索引记录，你可以在关联实例上调用 <code v-pre>searchable</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">orders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">searchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，如果你已经在内存中有一组 Eloquent 模型实例，你可以在集合实例上调用 <code v-pre>searchable</code> 方法，来更新它们对应的索引中的模型实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span><span class="token operator">-></span><span class="token function">searchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="在导入前修改记录" tabindex="-1"><a class="header-anchor" href="#在导入前修改记录"><span>在导入前修改记录</span></a></h4>
<p>有时候，在使模型可搜索之前，你可能需要准备好模型的集合。例如，你可能希望预加载一个关联，以便将关联数据高效地添加到你的搜索索引中。为实现这一目的，在相应的模型上定义一个 <code v-pre>makeSearchableUsing</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Collection</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 修改正在使其可搜索的模型集合。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">makeSearchableUsing</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Collection</span> <span class="token variable">$models</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Collection</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$models</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'author'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="移除记录" tabindex="-1"><a class="header-anchor" href="#移除记录"><span>移除记录</span></a></h3>
<p>要从索引中移除记录，你可以简单地从数据库中 <code v-pre>delete</code> 模型。即使你使用了<a href="https://learnku.com/docs/laravel/11.x/eloquent#soft-deleting" target="_blank" rel="noopener noreferrer">软删除</a>模型，也可以这样做：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$order</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$order</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你不想在删除记录之前检索模型，你可以在 Eloquent 查询实例上使用 <code v-pre>unsearchable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unsearchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想移除关系中所有模型的搜索索引记录，你可以在关联实例上调用 <code v-pre>unsearchable</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">orders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unsearchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，如果你已经在内存中有一组 Eloquent 模型实例，你可以在集合实例上调用 <code v-pre>unsearchable</code> 方法，从它们对应的索引中移除模型实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span><span class="token operator">-></span><span class="token function">unsearchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要从它们对应的索引中移除所有模型记录，你可以调用 <code v-pre>removeAllFromSearch</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">removeAllFromSearch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="暂停索引" tabindex="-1"><a class="header-anchor" href="#暂停索引"><span>暂停索引</span></a></h3>
<p>有时候，你可能需要对模型执行一批 Eloquent 操作，而不将模型数据同步到你的搜索索引中。你可以使用 <code v-pre>withoutSyncingToSearch</code> 方法来实现这一点。该方法接受一个闭包，该闭包将立即执行。闭包内发生的任何模型操作都不会被同步到模型的索引中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">withoutSyncingToSearch</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 执行模型操作...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="有条件地使模型实例可搜索" tabindex="-1"><a class="header-anchor" href="#有条件地使模型实例可搜索"><span>有条件地使模型实例可搜索</span></a></h3>
<p>有时候，你可能只想在特定条件下使模型可搜索。例如，假设你有一个 <code v-pre>App\Models\Post</code> 模型，该模型可能处于 「草稿」 和 「已发布」 两种状态之一。你可能只想允许 「已发布」 的帖子可搜索。为实现这一目的，你可以在你的模型上定义一个 <code v-pre>shouldBeSearchable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 确定模型是否应该可搜索。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">shouldBeSearchable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">isPublished</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>shouldBeSearchable</code> 方法仅在通过 <code v-pre>save</code> 和 <code v-pre>create</code> 方法、查询或关联操作模型时应用。直接使用 <code v-pre>searchable</code> 方法使模型或集合可搜索将覆盖 <code v-pre>shouldBeSearchable</code> 方法的结果。</p>
<blockquote>
<p><strong>注意</strong><br>
当使用 Scout 的 &quot;database&quot; 引擎时，<code v-pre>shouldBeSearchable</code> 方法不适用，因为所有可搜索数据始终存储在数据库中。当使用数据库引擎时，为了实现类似的行为，你应该使用<a href="#where-clauses">where子句</a>。</p>
</blockquote>
<h2 id="搜索" tabindex="-1"><a class="header-anchor" href="#搜索"><span>搜索</span></a></h2>
<p>你可以使用 <code v-pre>search</code> 方法开始搜索模型。<code v-pre>search</code> 方法接受一个字符串，该字符串将用于搜索你的模型。然后，你应该在搜索查询上链接 <code v-pre>get</code> 方法，以检索与给定搜索查询匹配的 Eloquent 模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于 Scout 搜索返回一个 Eloquent 模型集合，你甚至可以直接从路由或控制器返回结果，它们将自动转换为 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/search'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">search</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想在将搜索结果转换为 Eloquent 模型之前获取原始搜索结果，你可以使用 <code v-pre>raw</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="自定义索引" tabindex="-1"><a class="header-anchor" href="#自定义索引"><span>自定义索引</span></a></h4>
<p>搜索查询通常将在模型的 <a href="#configuring-model-indexes"><code v-pre>searchableAs</code></a> 方法指定的索引上执行。但是，你可以使用 <code v-pre>within</code> 方法指定应该搜索的自定义索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">within</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tv_shows_popularity_desc'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="where子句" tabindex="-1"><a class="header-anchor" href="#where子句"><span>Where子句</span></a></h3>
<p>Scout 允许你向搜索查询添加简单的 「where」 子句。目前，这些子句仅支持基本的数值相等性检查，并且主要用于通过所有者 ID 对搜索查询进行范围限定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，<code v-pre>whereIn</code> 方法可用于验证给定列的值是否包含在给定数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whereIn</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'open'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'paid'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>whereNotIn</code> 方法验证给定列的值是否不包含在给定数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whereNotIn</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'closed'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于搜索索引不是关系数据库，目前不支持更高级的 「where」 子句。</p>
<blockquote>
<p><strong>注意</strong><br>
如果你的应用程序使用 Meilisearch，则必须在使用 Scout 的 「where」 子句之前配置应用程序的<a href="#configuring-filterable-data-for-meilisearch">可过滤属性</a>。</p>
</blockquote>
<h3 id="分页" tabindex="-1"><a class="header-anchor" href="#分页"><span>分页</span></a></h3>
<p>除了检索模型集合之外，你还可以使用 <code v-pre>paginate</code> 方法对搜索结果进行分页。该方法将返回一个 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例，就像你对传统的 Eloquent 查询进行分页一样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过将数量作为 <code v-pre>paginate</code> 方法的第一个参数来指定每页检索多少个模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦你获取了结果，你可以使用 <a href="https://learnku.com/docs/laravel/11.x/blade" target="_blank" rel="noopener noreferrer">Blade</a> 显示结果并渲染页面链接，就像对传统的 Eloquent 查询进行分页一样：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>container<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">    @foreach ($orders as $order)</span>
<span class="line">        {{ $order->price }}</span>
<span class="line">    @endforeach</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span>
<span class="line">{{ $orders->links() }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，如果你想将分页结果作为 JSON 返回，你可以直接从路由或控制器返回分页器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/orders'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'query'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
由于搜索引擎不了解你的 Eloquent 模型的全局作用域定义，因此在使用 Scout 分页的应用程序中不应该使用全局作用域。或者，在通过 Scout 进行搜索时，你应该重新创建全局作用域的约束。</p>
</blockquote>
<h3 id="软删除" tabindex="-1"><a class="header-anchor" href="#软删除"><span>软删除</span></a></h3>
<p>如果你的索引模型正在进行<a href="https://learnku.com/docs/laravel/11.x/eloquent#soft-deleting" target="_blank" rel="noopener noreferrer">软删除</a>，并且你需要搜索软删除的模型，将 <code v-pre>config/scout.php</code> 配置文件中的 <code v-pre>soft_delete</code> 选项设置为 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'soft_delete'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当此配置选项为 <code v-pre>true</code> 时，Scout 不会从搜索索引中删除软删除的模型。相反，它会在索引记录上设置一个隐藏的 <code v-pre>__soft_deleted</code> 属性。然后，当搜索时，你可以使用 <code v-pre>withTrashed</code> 或 <code v-pre>onlyTrashed</code> 方法检索软删除的记录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 在检索结果时包括已删除的记录...</span></span>
<span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withTrashed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 在检索结果时仅包括已删除的记录...</span></span>
<span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">onlyTrashed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
当使用 <code v-pre>forceDelete</code> 永久删除软删除的模型时，Scout 将自动从搜索索引中删除它。</p>
</blockquote>
<h3 id="自定义引擎搜索" tabindex="-1"><a class="header-anchor" href="#自定义引擎搜索"><span>自定义引擎搜索</span></a></h3>
<p>如果你需要对引擎的搜索行为进行高级定制，你可以将闭包作为 <code v-pre>search</code> 方法的第二个参数传递。例如，你可以使用此回调在将搜索查询传递给 Algolia 之前向搜索选项添加地理位置数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Algolia<span class="token punctuation">\</span>AlgoliaSearch<span class="token punctuation">\</span>SearchIndex</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SearchIndex</span> <span class="token variable">$algolia</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$query</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$options</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'body'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'query'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bool'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'filter'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'geo_distance'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'distance'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'1000km'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'location'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'lat'</span> <span class="token operator">=></span> <span class="token number">36</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'lon'</span> <span class="token operator">=></span> <span class="token number">111</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$algolia</span><span class="token operator">-></span><span class="token function">search</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义-eloquent-结果查询" tabindex="-1"><a class="header-anchor" href="#自定义-eloquent-结果查询"><span>自定义 Eloquent 结果查询</span></a></h4>
<p>在 Scout 从你的应用程序搜索引擎中检索到一组匹配的 Eloquent 模型之后，Eloquent 会通过它们的主键检索所有匹配的模型。你可以通过调用 <code v-pre>query</code> 方法来自定义此查询。<code v-pre>query</code> 方法接受一个闭包作为参数，该闭包将接收 Eloquent 查询构建器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Builder</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Star Trek'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'invoices'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于此回调在相关模型已经从你的应用程序搜索引擎中检索到后被调用，因此 <code v-pre>query</code> 方法不应该用于“过滤”结果。相反，你应该使用<a href="#where%E5%AD%90%E5%8F%A5">Scout where子句</a>。</p>
<h2 id="自定义引擎" tabindex="-1"><a class="header-anchor" href="#自定义引擎"><span>自定义引擎</span></a></h2>
<h4 id="编写引擎" tabindex="-1"><a class="header-anchor" href="#编写引擎"><span>编写引擎</span></a></h4>
<p>如果内置的 Scout 搜索引擎之一不符合你的需求，你可以编写自己的自定义引擎并将其注册到 Scout 中。你的引擎应该扩展 <code v-pre>Laravel\Scout\Engines\Engine</code> 抽象类。这个抽象类包含你的自定义引擎必须实现的八个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>Builder</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">update</span><span class="token punctuation">(</span><span class="token variable">$models</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">delete</span><span class="token punctuation">(</span><span class="token variable">$models</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">search</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$builder</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">paginate</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$builder</span><span class="token punctuation">,</span> <span class="token variable">$perPage</span><span class="token punctuation">,</span> <span class="token variable">$page</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">mapIds</span><span class="token punctuation">(</span><span class="token variable">$results</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">map</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$builder</span><span class="token punctuation">,</span> <span class="token variable">$results</span><span class="token punctuation">,</span> <span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getTotalCount</span><span class="token punctuation">(</span><span class="token variable">$results</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">flush</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可能会发现审查 <code v-pre>Laravel\Scout\Engines\AlgoliaEngine</code> 类中这些方法的实现对你有所帮助。这个类将为你提供一个很好的起点，帮助你学习如何在自己的引擎中实现每个方法。</p>
<h4 id="注册引擎" tabindex="-1"><a class="header-anchor" href="#注册引擎"><span>注册引擎</span></a></h4>
<p>一旦你编写了自定义引擎，你可以使用 Scout 引擎管理器的 <code v-pre>extend</code> 方法将其注册到 Scout 中。Scout 的引擎管理器可以从 Laravel 服务容器中解析。你应该在 <code v-pre>App\Providers\AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法或应用程序使用的任何其他服务提供者中调用 <code v-pre>extend</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>ScoutExtensions<span class="token punctuation">\</span>MySqlSearchEngine</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Scout<span class="token punctuation">\</span>EngineManager</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Bootstrap any application services.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token class-name static-context">EngineManager</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mysql'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MySqlSearchEngine</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你的引擎被注册，你可以在应用程序的 <code v-pre>config/scout.php</code> 配置文件中将其指定为默认的 Scout <code v-pre>driver</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mysql'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/scoutmd/16733" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/sc...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/scoutmd/16733" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/sc...</a></p>
</blockquote>
</div></template>


